package com.imay.live.activity.discovery;

import android.content.Context;
import android.support.v4.util.LongSparseArray;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.UserInfoModel;
import com.imay.live.model.UserModel;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoRequestParams;
import com.imay.live.utils.NetWorkManager;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;

/**
 * Created by liangzhenxiong on 2017/04/15 0015.
 * 对于关注的处理事件
 */

public class FollowControl {
    LongSparseArray<String> uids;//保存点击了的在网络请求的uid
    UserModel myself;
    private synchronized void removeUid(String command) {
        long key = 0;
        for (int i = 0; i < uids.size(); i++) {
            key = uids.keyAt(i); // get the object by the key.
            if (command.equals(uids.get(key))) {
                uids.remove(key);
                return;
            }
        }
    }

    /**
     * 控制点击时候的操作
     *
     * @param context
     * @param userModel
     * @param toast
     */
    public void followChanged(Context context, UserModel userModel, boolean toast) {
        if (NetWorkManager.isNetworkAvailable(context)) {
            if (uids == null) {
                uids = new LongSparseArray<>();
            }
            if (myself != null) {
                if (myself.getUid() == userModel.getUid()) {
                    ToastUtil.show(context, context.getResources().getString(R.string.follow_myself));
                    return;
                }
            } else {
                UserInfoModel userInfoModel = UserInfoCache.getUserInfo();
                if (userInfoModel != null) {
                    myself = userInfoModel.getUser();
                    if (myself != null && myself.getUid() == userModel.getUid()) {
                        ToastUtil.show(context, context.getResources().getString(R.string.follow_myself));
                        return;
                    }
                }
            }
            if (userModel != null) {
                ProtoRequestParams protoRequestParams;
                synchronized (this) {
                    if (uids.get(userModel.getUid()) != null) {
                        return ;
                    }

                    if (!userModel.isMyFollow()) {
                        protoRequestParams = ProtoHttpFactory.createFollow(userModel.getUid());
                        uids.put(userModel.getUid(), protoRequestParams.getCommand());
                        userModel.setMyFollow(true);
                        if (toast) {
                            ToastUtil.show(context, context.getResources().getString(R.string.follow_success));
                        }
                    } else {
                        protoRequestParams = ProtoHttpFactory.cancleFollow(userModel.getUid());
                        uids.put(userModel.getUid(), protoRequestParams.getCommand());
                        userModel.setMyFollow(false);
                        if (toast) {
                            ToastUtil.show(context, context.getResources().getString(R.string.follow_cancle));
                        }
                    }
                }

                EventHelper.postFollowChange(userModel.getUid(), userModel.isMyFollow());
                ProtoRequestHelper.task(protoRequestParams).setTag(this.hashCode()).request(new ProtoCallBack() {
                    @Override
                    public void onRequestSuccess(String command, String data) {
                        removeUid(command);
                    }

                    @Override
                    public void onRequestError(String command, int code, String message) {
                        removeUid(command);
                    }
                });
                return ;
            }
        }
        return ;
    }

    /**
     * 控制点击时候的操作
     *
     * @param context
     * @param userModel
     */
    public void followChanged(Context context, UserModel userModel) {
        followChanged(context, userModel, true);
    }

    public View getViewByPosition(RecyclerView recyclerView, int position, int viewId) {
        if (recyclerView == null) {
            return null;
        }
        BaseViewHolder viewHolder = (BaseViewHolder) recyclerView.findViewHolderForLayoutPosition(position);
        if (viewHolder == null) {
            return null;
        }
        return viewHolder.getView(viewId);
    }

}
